package com.example.health_life.service;

import com.example.health_life.dao.Orderitem;
import com.example.health_life.dao.OrderitemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderitemServiceImpl implements OrderitemService {

    @Autowired
    private OrderitemRepository orderitemRepository;

    @Override
    public List<Orderitem> getOrderitemList(Integer orderId) {
        if (orderId != null) {
            return orderitemRepository.findByOrderId(orderId);
        } else {
            return orderitemRepository.findAll();
        }
    }

    @Override
    public Orderitem getOrderitemById(Integer id) {
        return orderitemRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("订单项不存在"));
    }

    @Override
    @Transactional
    public int addOrderitem(Orderitem orderitem) {
        // 验证必填字段
        if (orderitem.getOrderId() == null) {
            throw new IllegalStateException("订单ID不能为空");
        }
        if (orderitem.getProductId() == null) {
            throw new IllegalStateException("商品ID不能为空");
        }
        if (orderitem.getProductCount() == null || orderitem.getProductCount() <= 0) {
            throw new IllegalStateException("商品数量必须大于0");
        }
        if (orderitem.getTotal() == null || orderitem.getTotal().doubleValue() <= 0) {
            throw new IllegalStateException("商品总价必须大于0");
        }

        return orderitemRepository.save(orderitem).getId();
    }

    @Override
    @Transactional
    public int updateOrderitem(Integer id, Integer quantity, Double price, Integer productId) {
        Orderitem orderitem = orderitemRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("订单项不存在"));

        // 更新订单项信息
        if (quantity != null) {
            if (quantity <= 0) {
                throw new IllegalStateException("商品数量必须大于0");
            }
            orderitem.setProductCount(quantity);
        }
        if (price != null) {
            if (price <= 0) {
                throw new IllegalStateException("商品价格必须大于0");
            }
            orderitem.setTotal(java.math.BigDecimal.valueOf(price));
        }
        if (productId != null) {
            orderitem.setProductId(productId);
        }

        orderitemRepository.save(orderitem);
        return id;
    }

    @Override
    @Transactional
    public int deleteOrderitem(Integer id) {
        Orderitem orderitem = orderitemRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("订单项不存在"));
        
        orderitemRepository.delete(orderitem);
        return id;
    }

    @Override
    @Transactional
    public int deleteOrderitemsByOrderId(Integer orderId) {
        List<Orderitem> orderitems = orderitemRepository.findByOrderId(orderId);
        if (orderitems.isEmpty()) {
            return 0;
        }
        
        orderitemRepository.deleteAll(orderitems);
        return orderitems.size();
    }
}
